/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.result;

import com.jspx.boot.environment.Environment;
import com.jspx.json.JSONException;
import com.jspx.json.XML;
import com.jspx.json.XMLParserConfiguration;
import com.jspx.txweb.ActionInvocation;
import com.jspx.txweb.dispatcher.Dispatcher;
import com.jspx.txweb.support.ActionSupport;
import com.jspx.json.JSONObject;
import com.jspx.txweb.util.TXWebUtil;
import com.jspx.utils.*;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 2009-6-30
 * Time: 12:25:17
 * 判断是否使用 Key_resultMethods  配置的格式返回json,还是更具rpc调用方式
 * com.jspx.txweb.result.RocResult
 * <p>
 * <p>
 * 信息显示默认方法
 * if (msg.success==1)
 * {
 * alert("成功");
 * } else
 * {
 * alert(msg.error.message);
 * }
 */

public class RocResult extends ResultSupport {

    public RocResult() {

    }


    public void execute(ActionInvocation actionInvocation) throws JSONException {
        ActionSupport action = actionInvocation.getActionProxy().getAction();
        HttpServletResponse response = action.getResponse();
        checkCache(action,response);

        JSONObject json = getRocAutoResult(actionInvocation);

        //如果头部设置为javascript mootools IE下会自动执行
        boolean outXML = false;
        JSONObject callJson = actionInvocation.getActionProxy().getCallJson();
        if (callJson != null && KEY_RocFormatXML.equalsIgnoreCase(callJson.getString(Environment.rocFormat))) {
            outXML = true;
        }

        if (outXML) {

            String result = XML.toString(json, Environment.rocResult, new XMLParserConfiguration());
            if (debug) result = XMLUtil.format(result);
            TXWebUtil.print("<?xml version=\"1.0\" encoding=\"" + Dispatcher.getEncode() + "\"?>\r\n" + result, TXWebUtil.xmlType, response);
        } else {
            String result = debug ? json.toString(4) : json.toString();
            TXWebUtil.print(result, TXWebUtil.jsonType, response);
        }
        if (json != null) json.clear();
    }


}